package net.cyue.android.js.module;

import android.view.View;

import com.kongzue.dialogx.dialogs.InputDialog;
import com.kongzue.dialogx.dialogs.MessageDialog;
import com.wragony.android.jsbridge.module.JSBridgeMethod;
import com.wragony.android.jsbridge.module.JsModule;
import com.wragony.android.jsbridge.module.datatype.JBCallback;

public class DialogXJSModule extends JsModule
{

    @Override
    public String getModuleName() {
        return "DialogX";
    }


    @JSBridgeMethod
    public void message(String title, String message, JBCallback okCallback)
    {
        MessageDialog.build()
            .setCancelable(false)
            .setTitle(title)
            .setMessage(message)
            .setOkButton("确定")
            .setCancelButton("取消")
            .show()
            .setOkButton((MessageDialog dialog, View v) -> {
                if (okCallback != null)
                    okCallback.apply();
                return false;
            });
    }

    @JSBridgeMethod
    public void messageCancelable(String title, String message, JBCallback okCallback)
    {
        MessageDialog.build()
            .setCancelable(true)
            .setTitle(title)
            .setMessage(message)
            .setOkButton("确定")
            .show()
            .setOkButton((MessageDialog dialog, View v) -> {
                if (okCallback != null)
                    okCallback.apply();
                return false;
            });
    }

    @JSBridgeMethod
    public void messageCancelableCustom(
            String title,
            String message,
            String okText,
            String cancelText,
            JBCallback okCallback,
            JBCallback cancelCallback
        )
    {
        MessageDialog.build()
            .setCancelable(true)
            .setTitle(title)
            .setMessage(message)
            .setOkButton(okText)
            .setCancelButton(cancelText)
            .show()
            .setOkButton((MessageDialog dialog, View v) -> {
                if (okCallback != null)
                    okCallback.apply();
                return false;
            })
            .setCancelButton((MessageDialog dialog, View v) -> {
                if (cancelCallback != null)
                    cancelCallback.apply();
                return false;
            });
    }


    @JSBridgeMethod
    public void input(String title, String message, JBCallback okCallback)
    {
        new InputDialog(title, message, "确定", "取消")
            .setCancelable(false)
            .setOkButton((InputDialog baseDialog, View v, String inputStr) -> {
                if (okCallback != null)
                    okCallback.apply(inputStr);
                return false;
            })
            .show();
    }

}

